<template>
    <div class="fastdata-dbscript-container">
        <el-dialog title="新增数据脚本" v-model="isShowDialog" >
            <el-form ref="addDbscriptFormRef" :model="formData" :rules="formRules" label-width="80px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="功能名称" prop="business">
                            <el-input v-model="formData.business" placeholder="请输入功能名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="模块名称" prop="module">
                            <el-input v-model="formData.module" placeholder="请输入模块名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="数据源" prop="theme">
							<el-select v-model="formData.dbsourceId" placeholder="请选择数据源" clearable class="w100">
                                <el-option
									v-for="item in dbsources"
									:key="item.id"
									:label="item.name"
									:value="item.id">
								</el-option>
							</el-select>
						</el-form-item>	
					</el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="主题背景" prop="theme">
							<el-select v-model="codemirror.options.theme" placeholder="请选择编辑器主题" clearable class="w100">
                                <el-option label="blackboard" value="blackboard"></el-option>
                                <el-option label="idea" value="idea"></el-option>
                                <el-option label="material" value="material"></el-option>
                                <el-option label="night" value="night"></el-option>
							</el-select>
						</el-form-item>	
					</el-col>
                    <el-col style="min-height: 200px;">
                        <Codemirror ref="codemirrorRef" v-model:value="formData.sqlScript" :options="codemirror.options" border/>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import Codemirror from "codemirror-editor-vue3";
import 'codemirror/theme/blackboard.css';
import 'codemirror/theme/idea.css';
import 'codemirror/theme/material.css';
import 'codemirror/theme/night.css';
import "codemirror/mode/sql/sql.js";
import { addDbscript } from '/@/api/fastdata/dbscript/index';
import { getDbsourceList } from '/@/api/fastdata/dbsource/index';
export default {
    name: 'fastdata/dbscript/add',
    components: { Codemirror },
    setup(props, context) {
        const addDbscriptFormRef = ref();
        const codemirrorRef = ref();
        const state = reactive({
            isShowDialog: false,
            dbsources: [],
            formData: {
                sqlScript: `<!-- 请输入SQL脚本，脚本语言支持Mybatils动态脚本语言，例如： -->
select * from mytable 
<where>
    <if test="id != null">
        and id = #{id} 
    </if>
    <if test="status != null">
        and status = #{status}    
    </if>
</where>
order by create_time desc`,
            },
            formRules : {
                business: { required: true, message: '功能名称不能为空', trigger: 'blur' },
                module: { required: true, message: '模块名称不能为空', trigger: 'blur' },
                dbsourceId: { required: true, message: '数据源不能为空', trigger: 'change' },
            },
            codemirror: {
				options: {
					theme: 'idea',
					mode: "sql",
					line: true,
					lineWrapping: true,
					lineNumbers: true, // 是否在编辑器左侧显示行号
					matchBrackets: true, // 括号匹配
					styleActiveLine: true ,// 光标行高亮
					extraKeys:{"Ctrl-Space":"autocomplete"},
					gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"],
				}
			}
        });

        
        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
            getDbsourceList({pageNum: 1, pageSize: 1000}).then((res) => {
                if(res.code == 0){
                    state.dbsources = res.data;
                }
            });
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            addDbscriptFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            addDbscriptFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        business: state.formData.business,
                        module: state.formData.module,
                        dbsourceId: state.formData.dbsourceId,
                        sqlScript: state.formData.sqlScript,
                        remark: state.formData.remark,
                    }
                    addDbscript(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            addDbscriptFormRef,
            codemirrorRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            ...toRefs(state),
        };
    },
};
</script>